<template>
  <div class="app-container">

    <!-- 查询条件 -->
    <el-form size="small" :inline="true" :model="queryParam" class="demo-form-inline">
      <el-form-item label="分类名">
        <el-input v-model="queryParam.name" placeholder="分类名" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="btnSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd()">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate()">修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete()">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini">导出</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableData.rows" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="id" label="编号" width="80" />
      <el-table-column prop="merName" label="商家名称" width="300" />
      <el-table-column prop="name" label="分类名称" width="260" />
      <el-table-column label="分类图" width="160">
        <template slot-scope="scope">
          <img v-if="scope.row.picture" :src="scope.row.picture" width="150">
          <el-tag v-else type="danger">未上传图像</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      v-if="tableData.total>0"
      :current-page="queryParam.page"
      :page-sizes="[1, 5, 10, 20,30,50,100]"
      :page-size="queryParam.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!--新增修改对话框      :close-on-click-modal 点击对话框外不会关闭   -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :before-close="cancelForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类图片" prop="picture">
          <el-upload
            class="avatar-uploader"
            :action="imageUploadUrl"
            :show-file-list="false"
            :headers="headers"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.picture" :src="form.picture" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>

          <p v-show="false">
            <el-input v-model="form.picture" type="hidden" />
          </p>
        </el-form-item>
        <el-button style="margin-left: 300px" @click="cancelForm">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import PageMinix from '@/mixin/PageMixin.js'
import AddOrEditMixin from '@/mixin/AddOrEditMixin.js'
import { dataListApi, addApi, editApi, findById, deleteByIdApi } from '@/api/category.js'
export default {
  mixins: [PageMinix, AddOrEditMixin],
  data() {
    return {
      fetchDataApi: dataListApi,
      addApi: addApi,
      editApi: editApi,
      findByIdApi: findById,
      deleteByIdApi: deleteByIdApi,
      primaryKey: 'id',
      rules: {
        name: [
          { required: true, message: '该值不能为空', trigger: 'blur' }
        ],
        picture: [
          { required: true, message: '该值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initFormValue()
  },
  methods: {
    
    // 表单初始化值
    initFormValue() {
      this.form = {
        picture: undefined
      }
    },
    // 图片上传返回解析
    handleAvatarSuccess(res, file) {
      console.log(res)
      if (res.code !== 200) {
        this.$message.error(res.msg)
      } else {
        this.form.picture = 'http://localhost:8080' + res.data.image
      }
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
